package LatentTicTacToe.Players;

import java.util.HashMap;
import java.util.Random;

import LatentTicTacToe.Calculators.LatentTicTacToeTreeUpdater;
import LatentTicTacToe.Data.AbstractBoard;
import LatentTicTacToe.Data.Move;
import LatentTicTacToe.InformationSets.CFRInformationSet;
import LatentTicTacToe.misc.GameTreeBuilder;

/**
 * Class which implements counterfactual regret minimizing player
 * 
 * @author Jiri Cermak
 * 
 */
public class CFRPlayer extends Player {

	private HashMap<String, CFRInformationSet> informationSets;

	public CFRPlayer(AbstractBoard initialBoard, byte sign) {
		super(sign, initialBoard);
		this.informationSets = new GameTreeBuilder(new LatentTicTacToeTreeUpdater(), initialBoard).getGameTree();
	}

	@Override
	public Move getNextNode(AbstractBoard situation, boolean validity) {

		Random random = new Random();
		CFRInformationSet set = informationSets.get(situation.id);

		float move = random.nextFloat();

//		System.out.println();
//		System.out.println(set);
//		System.out.println();

		for (int i = 0; i < set.getStrategy().length; i++) {
			move -= set.getStrategy()[i];

			if (move < 0) {
				return set.moves.get(i);
			}

		}

		return set.moves.getLast();
	}

}
